package com.ms.qw.application.validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.validator.ValidationException;
import org.springframework.batch.item.validator.Validator;
import org.springframework.beans.factory.InitializingBean;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * User: Matrix
 * Date: 10/02/13
 */
@SuppressWarnings("rawtypes")
public class BeanValidator implements Validator, InitializingBean {
    private static final Logger LOG = LoggerFactory.getLogger(BeanValidator.class);
    private javax.validation.Validator validator;
    public void afterPropertiesSet() throws Exception {
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        validator = validatorFactory.usingContext().getValidator();

    }
    public void validate(Object target) throws ValidationException {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(target);
        if(constraintViolations.size() > 0) {
            LOG.error(target.toString());
            buildValidationException(constraintViolations);
        }
    }
    private void buildValidationException(
            Set<ConstraintViolation<Object>> constraintViolations) {
        StringBuilder message = new StringBuilder();
        for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
            message.append(constraintViolation.getMessage() + "\n");
        }
        throw new ValidationException(message.toString());
    }
}